package com.fr.chicken.service.Impl;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fr.chicken.dao.*;
import com.fr.chicken.facade.base.BaseResponseDTO;
import com.fr.chicken.facade.info.PlayerListInfo;
import com.fr.chicken.facade.request.*;
import com.fr.chicken.facade.response.*;
import com.fr.chicken.manager.account.AccountManager;
import com.fr.chicken.manager.house.HouseManager;
import com.fr.chicken.manager.vip.VipManager;
import com.fr.chicken.model.*;
import com.fr.chicken.service.AdminService;
import com.fr.chicken.util.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminDao adminDao;
    @Resource
    private AdminLogDao adminLogDao;
    @Autowired
    private ChickenConfigureDao chickenConfigureDao;
    @Autowired
    private MaxConfigureDao maxConfigureDao;
    @Autowired
    private ExchangeConfigureDao exchangeConfigureDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private ChickenInfoDao chickenInfoDao;
    @Autowired
    private ActivationLogDao activationLogDao;
    @Autowired
    private HouseManager houseManager;
    @Autowired
    private AccountManager accountManager;
    @Autowired
    private VipConfigureDao vipConfigureDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public AdminLonginResponseDTO login(AdminLoginRequsetDTO requset) {

            AdminLonginResponseDTO response = new AdminLonginResponseDTO();
            Admin admin = adminDao.selectByAdminName(requset.getAdminname());
            if (admin==null){
                response.setIsLogin(false);
                response.setMsg("用户不存在");
                response.faild();
                return response;
            }
            String password = Md5Util.getMD5(requset.getPassword()).toLowerCase();
            if (admin.getPassword().equals(password)){
                response.setIsLogin(true);
            }else {
                response.setIsLogin(false);
                response.setMsg("密码错误");
                response.faild();
                return response;
            }
            AdminLog adminLog = new AdminLog();
            adminLog.setAdminId(admin.getId());
            adminLog.setLogTime(new Date());
            adminLog.setLoginIp(requset.getLoginIp());
            adminLog.setLogInfo("登录");
            adminLog.setOperation("登录后台");

            int num = adminLogDao.insertSelective(adminLog);
            AdminLog adminLog1 = adminLogDao.selectByAdminIdAndNewest(admin.getId());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String lastLoginTime = sdf.format(adminLog1.getLogTime());
            response.setLastLoginTime(lastLoginTime);
            response.setIp(adminLog1.getLoginIp());
            if (num < 1) {
                response.faild();
                response.setMsg("插入登录日志失败");
                return response;
            } else {
                response.success();
                response.setMsg("登录成功");
                return response;
            }
    }

    @Override
    public GetChickenConfigureResponseDTO getChickenConfigure() {
        GetChickenConfigureResponseDTO responseDTO = new GetChickenConfigureResponseDTO();
        List<ChickenConfigure> chickenConfigures = chickenConfigureDao.selectAll();
        responseDTO.setChickenConfigures(chickenConfigures);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateChickenConfigure(UpdateChickenConfigureRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        ChickenConfigure chickenConfigure = JSONObject.parseObject(JSON.toJSONString(requsetDTO),ChickenConfigure.class);
        int num = chickenConfigureDao.updateByPrimaryKeySelective(chickenConfigure);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public GetMaxConfigureResponseDTO getMaxConfigure() {
        GetMaxConfigureResponseDTO responseDTO = new GetMaxConfigureResponseDTO();
        List<MaxConfigure> maxConfigures = maxConfigureDao.selectAll();
        responseDTO.setMaxConfigures(maxConfigures);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateMaxConfigure(UpdateMaxConfigureRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        MaxConfigure maxConfigure = JSONObject.parseObject(JSON.toJSONString(requsetDTO),MaxConfigure.class);
        int num = maxConfigureDao.updateByPrimaryKeySelective(maxConfigure);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateExchangeConfigure(UpdateExchangeConfigureRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        ExchangeConfigure exchangeConfigure = JSONObject.parseObject(JSON.toJSONString(requsetDTO),ExchangeConfigure.class);
        int num = exchangeConfigureDao.updateByPrimaryKeySelective(exchangeConfigure);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO insertExchangeConfigure(InsertExchangeConfigureRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        ExchangeConfigure exchangeConfigure = JSONObject.parseObject(JSON.toJSONString(requsetDTO),ExchangeConfigure.class);
        int num = exchangeConfigureDao.insertSelective(exchangeConfigure);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("添加失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("添加成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO deleteExchangeConfigure(Integer id) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        int num = exchangeConfigureDao.deleteByPrimaryKey(id);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("删除失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("删除成功");
        return responseDTO;
    }

    @Override
    public GetPlatformDataResponseDTO getPlatformData() {
        GetPlatformDataResponseDTO responseDTO = new GetPlatformDataResponseDTO();
        List<PlayerInfo> playerInfos = playerInfoDao.selectAll();
        responseDTO.setUserNums(playerInfos.size());
        List<ChickenInfo> chickenInfos = chickenInfoDao.selectAll();
        responseDTO.setChickenNums(chickenInfos.size());
        int eggNums = 0;
        for (PlayerInfo x:playerInfos){
              eggNums=eggNums+x.getEgg();
        }
        responseDTO.setEggNums(eggNums);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateAdminPassword(UpdateAdminPasswordRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        Admin admin = adminDao.selectByAdminName(requsetDTO.getAdminname());
        if (admin==null){
            responseDTO.faild();
            responseDTO.setMsg("管理员不存在");
            return responseDTO;
        }
        String lastPassword = Md5Util.getMD5(requsetDTO.getLastPassword()).toLowerCase();
        if (!admin.getPassword().equals(lastPassword)){
            responseDTO.faild();
            responseDTO.setMsg("密码错误");
            return responseDTO;
        }
        String password = Md5Util.getMD5(requsetDTO.getPassword()).toLowerCase();
        admin.setPassword(password);
        int num = adminDao.updateByPrimaryKeySelective(admin);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public GetAdminLoginListResponseDTO getAdminLoginList(GetAdminLoginListRequsetDTO requsetDTO) {
        GetAdminLoginListResponseDTO responseDTO = new GetAdminLoginListResponseDTO();
        if (null != requsetDTO.getPageNum() && null != requsetDTO.getPageSize()) {
            PageHelper.startPage(requsetDTO.getPageNum(), requsetDTO.getPageSize());
        }
        List<AdminLog> adminLogs = adminLogDao.selectAll();
        PageInfo pageInfo = new PageInfo(adminLogs);
        pageInfo.setList(adminLogs);
        responseDTO.setPageInfo(pageInfo);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO updateUserNums(UpdateUserNumsRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
        int egg = requsetDTO.getEgg()-playerInfo.getEgg();
        int feather = requsetDTO.getFeather()-playerInfo.getFeather();
        int feaces = requsetDTO.getFeaces()-playerInfo.getFeaces();
        playerInfo.setEgg(requsetDTO.getEgg());
        playerInfo.setFeather(requsetDTO.getFeather());
        playerInfo.setFeaces(requsetDTO.getFeaces());
        int num = playerInfoDao.updateByPrimaryKeySelective(playerInfo);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        AdminLog adminLog = new AdminLog();
        adminLog.setAdminId(requsetDTO.getId());
        adminLog.setLogTime(new Date());
        adminLog.setLoginIp(requsetDTO.getLoginIp());
        adminLog.setLogInfo("更改用户数据");
        adminLog.setOperation("给用户"+requsetDTO.getPlayerId()+"增加了"+egg+"个鸡蛋"+","+feather+"个鸡毛"+","+feaces+"个鸡屎");
        int num1 = adminLogDao.insertSelective(adminLog);
        if (num1<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public GetPlayerInfoListResponseDTO getOneAgentInfoList(GetOneAgentInfoListRequsetDTO requsetDTO) {
        GetPlayerInfoListResponseDTO responseDTO = new GetPlayerInfoListResponseDTO();
        List<ActivationLog> activationLogs = activationLogDao.selectByInviterId(requsetDTO.getId());
        ArrayList<Long> ids = new ArrayList<>();
        for (ActivationLog activationLog:activationLogs){
                 ids.add(activationLog.getInviteeId());
        }
        if (ids==null||ids.size()<=0){
            responseDTO.success();
            responseDTO.setMsg("查询成功");
            return responseDTO;
        }
        if (null != requsetDTO.getPageNum() && null != requsetDTO.getPageSize()) {
            PageHelper.startPage(requsetDTO.getPageNum(), requsetDTO.getPageSize());
        }
        List<PlayerInfo> playerInfos = playerInfoDao.selectByIdsAndNickId(ids,requsetDTO.getPlayerId());
        PageInfo pageInfo = new PageInfo(playerInfos);
        List<PlayerListInfo> playerListInfos = new ArrayList<>();
        for (PlayerInfo x:playerInfos){
            PlayerListInfo playerListInfo = JSONObject.parseObject(JSON.toJSONString(x),PlayerListInfo.class);
            HouseInfo houseInfo = houseManager.getHouseByPlayerId(x.getId());
            List<ChickenInfo> chickenInfos = chickenInfoDao.selectByHouseId(houseInfo.getId());
            playerListInfo.setChickenNums(chickenInfos.size());
            List<ActivationLog> activationLogs1 = activationLogDao.selectByOneAgentId(x.getId());
            playerListInfo.setSubordinateNums(activationLogs1.size());
            List<AccountList> accountLists = accountManager.findAccountByPlayerId(x.getId());
            for (AccountList x1:accountLists){
                if (x1.getAccount().length()==11){
                    playerListInfo.setPhone(x1.getAccount());
                    break;
                }
            }
            playerListInfos.add(playerListInfo);
        }
        pageInfo.setList(playerListInfos);
        responseDTO.setPageInfo(pageInfo);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public GetVipConfigureResponseDTO getVipConfigure() {
        GetVipConfigureResponseDTO responseDTO = new GetVipConfigureResponseDTO();
        List<VipConfigure> vipConfigures = vipConfigureDao.selectAll();
        responseDTO.setVipConfigures(vipConfigures);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateVipConfigure(UpdateVipConfigureRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        VipConfigure vipConfigure = JSONObject.parseObject(JSON.toJSONString(requsetDTO),VipConfigure.class);
        int num = vipConfigureDao.updateByPrimaryKeySelective(vipConfigure);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }

    @Override
    public GetActivationCodeInfoResponseDTO getActivationCodeInfo() {
        GetActivationCodeInfoResponseDTO responseDTO = new GetActivationCodeInfoResponseDTO();
        Goods goods = goodsDao.selectByPrimaryKey(1);
        List<Goods> goodsList = new ArrayList<>();
        goodsList.add(goods);
        responseDTO.setGoods(goodsList);
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        return responseDTO;
    }

    @Override
    public BaseResponseDTO updateGoods(UpdateGoodsRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        Goods goods = JSONObject.parseObject(JSON.toJSONString(requsetDTO),Goods.class);
        goods.setCreateTime(System.currentTimeMillis() / 1000);
        int num = goodsDao.updateByPrimaryKeySelective(goods);
        if (num<1){
            responseDTO.faild();
            responseDTO.setMsg("修改失败");
            return responseDTO;
        }
        responseDTO.success();
        responseDTO.setMsg("修改成功");
        return responseDTO;
    }
}
